package com.example.back.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.stereotype.Component;
import sun.security.provider.MD5;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    private static final String charset = "utf-8";
    // 偏移量
    private static final int offset = 32;
    private static final String transformation = "AES/CBC/PKCS5Padding";
    private static final String algorithm = "AES";
    public static final String DEFAULT_KEY = "88d00e1d2cda6717799ddf9d8623363c";
    /**
     * 判断是否为同一天
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        return isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
    }

    public static int getDayOfWeek() {
        int num = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        int[] week = { 7, 1, 2, 3, 4, 5, 6 };
        return week[num - 1];
    }

    public static void main(String[] args) {

        String str="1";
        System.out.println(str=="1");
    }

    public static long fibonacci(long number) {
        if ((number == 0) || (number == 1))
            return number;
        else
            return fibonacci(number - 1) + fibonacci(number - 2);
    }

    public static String encrypt(String content, String key) {
        try {
            if (StringUtils.isBlank(content) || StringUtils.isBlank(key)) {
                System.out.println("AESUtil|encrypt|error|" + content + "|" + key);
                return null;
            }
            if (key.length() != offset) {
                System.out.println("AESUtil|encrypt|error|" + content + "|" + key);
                return null;
            }
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            byte[] byteContent = content.getBytes(charset);
            cipher.init(Cipher.ENCRYPT_MODE, skey, iv);
            byte[] result = cipher.doFinal(byteContent);
            return new Base64().encodeToString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String decrypt(String content, String key) {
        try {
            if (StringUtils.isBlank(content) || StringUtils.isBlank(key)) {
                System.out.println("AESUtil|decrypt|error|" + content + "|" + key);
                return null;
            }
            if (key.length() != offset) {
                System.out.println("AESUtil|decrypt|error|" + content + "|" + key);
                return null;
            }
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);
            byte[] result = cipher.doFinal(new Base64().decode(content));
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
